package com.ugrow.ugrowcommunity.activity.seatReservation.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.ugrow.ugrowcommunity.R;
import com.ugrow.ugrowcommunity.databinding.SeatItemBinding;
import com.ugrow.ugrowcommunity.util.ArrayExistUtil;

public class SeatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private int parentWidth;
    private int lineSize;
    private String[] list;
    private OnItemClickListener onItemClickListener;
    private int size;

    public SeatAdapter(Context context, int lineSize, int seatSize, String[] list ) {
        this.context = context;
        this.lineSize = lineSize;
        this.list = list;
        this.size = seatSize;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        parentWidth = parent.getWidth();
        if (viewType == SeatAdapter.viewType.SEAT.ordinal()) {
            return new SeatViewHolder(LayoutInflater.from(context).inflate(R.layout.seat_item, parent, false));
        } else if (viewType == SeatAdapter.viewType.LINE.ordinal()) {
            return new LineViewHolder(LayoutInflater.from(context).inflate(R.layout.item_dotted_line_interval, parent, false));
        } else return null;

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof SeatViewHolder) {
            holder.itemView.getLayoutParams().width = (parentWidth / lineSize);
            int seatPosition = position - position / (lineSize * 2);
            ((SeatViewHolder) holder).itemBinding.checkbox.setOnClickListener(v -> {
                onItemClickListener.onClickListener((CheckBox) v, seatPosition);
            });
            ((SeatViewHolder) holder).itemBinding.position.setText(String.valueOf(seatPosition));


            if (ArrayExistUtil.stringExistList(list, String.valueOf(position))) {
                ((SeatViewHolder) holder).itemBinding.checkbox.setChecked(true);
                ((SeatViewHolder) holder).itemBinding.checkbox.setClickable(false);
                ((SeatViewHolder) holder).itemBinding.checkbox.setAlpha(0.5f);
            } else {
                ((SeatViewHolder) holder).itemBinding.checkbox.setChecked(false);
            }
        } else if (holder instanceof LineViewHolder) {
            holder.itemView.getLayoutParams().width = parentWidth;
        }

    }

    public enum viewType {
        SEAT, LINE
    }

    @Override
    public int getItemViewType(int position) {
        if (isaBoolean(position)) {
            return viewType.LINE.ordinal();
        } else return viewType.SEAT.ordinal();
    }

    public boolean isaBoolean(int position) {
        return (position - position / (lineSize * 2)) % (lineSize * 2) == 0;
    }

    @Override
    public int getItemCount() {
        return size + size / (lineSize * 2);
    }

    public static class SeatViewHolder extends RecyclerView.ViewHolder {
        public SeatItemBinding itemBinding;

        public SeatViewHolder(@NonNull View itemView) {
            super(itemView);
            itemBinding = SeatItemBinding.bind(itemView);
        }
    }

    static class LineViewHolder extends RecyclerView.ViewHolder {
        public LineViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }


    public void setOnItemClickListener(OnItemClickListener listener) {
        onItemClickListener = listener;
    }


    public interface OnItemClickListener {
        void onClickListener(CheckBox checkBox, int position);
    }
}
